﻿#region

using System;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

#endregion

namespace Imdb
{
    [Serializable]
    public class CacheObject
    {
        #region Properties

        public DateTime Created { get; set; }

        public object Object { get; set; }

        #endregion Properties
    }

    /// <summary>
    ///     FileCache class
    /// </summary>
    public class FileCache
    {
        #region Constructors

        public FileCache()
        {
            CacheDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Cache");
            MaxDays = 7;
            createDirectory();
        }

        public FileCache(string cacheDirectory, int maxDays)
        {
            CacheDirectory = cacheDirectory;
            MaxDays = maxDays;
            createDirectory();
        }

        #endregion Constructors

        #region Properties

        public string CacheDirectory { get; set; }

        public int MaxDays { get; set; }

        #endregion Properties

        #region Methods

        public object Read(string name)
        {
            name = Path.Combine(CacheDirectory, name + ".cache");
            if (!File.Exists(name))
                return null;
            else
            {
                CacheObject Cache = Unserialize(name);
                return Cache.Created.AddDays(MaxDays) > DateTime.Now ? Cache.Object : null;
            }
        }

        public bool Save(string name, object value)
        {
            return Serialize(new CacheObject {Created = DateTime.Now, Object = value},
                             Path.Combine(CacheDirectory, name + ".cache"));
        }

        private void createDirectory()
        {
            if (!Directory.Exists(CacheDirectory))
                Directory.CreateDirectory(CacheDirectory);
        }

        private bool Serialize(object obj, string file)
        {
            try
            {
                var serializer = new BinaryFormatter {AssemblyFormat = FormatterAssemblyStyle.Simple};
                using (var fs = new FileStream(file, FileMode.OpenOrCreate))
                    serializer.Serialize(fs, obj);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private CacheObject Unserialize(string file)
        {
            try
            {
                var serializer = new BinaryFormatter();
                using (var fs = new FileStream(file, FileMode.Open))
                {
                    CacheObject Cache;
                    Cache = (CacheObject) serializer.Deserialize(fs);
                    return Cache;
                }
            }
            catch
            {
                return null;
            }
        }

        #endregion Methods
    }
}